package com.zhihuichongcheng.application.bean.task;

import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.util.CollectionUtils;
import com.zhihuichongcheng.application.util.StringUtils;

import java.util.List;

/**
 * SubTaskBean
 *
 * Created by LK on 2017/1/17.
 */
public class SubTaskBean {
    private int id;
    private String state;
    private int userMissionId;
    private String title;
    private String finishTime;
    private String receiveUserId;
    private int userId;
    private String userName;
    private List<UserBean> receiveUsers;

    public int getSubTaskId() {
        return id;
    }

    public void setSubTaskId(int id) {
        this.id = id;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getStateMsg() {
        if (state.equals("1")) {
            return "进行中";
        } else if (state.equals("2")) {
            return "已完成";
        }
        return state;
    }

    public void setUserMissionId(int userMissionId) {
        this.userMissionId = userMissionId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(String finishTime) {
        this.finishTime = finishTime;
    }

    public String getReceiveUserId() {
        return receiveUserId;
    }

    public void setReceiveUserId(String receiveUserId) {
        this.receiveUserId = receiveUserId;
    }

    public List<UserBean> getReceiveUsers() {
        return receiveUsers;
    }

    public void setReceiveUsers(List<UserBean> receiveUsers) {
        this.receiveUsers = receiveUsers;
    }

    public String getExecutorNicknames() {
        return getUserNames(receiveUsers);
    }

    private String getUserNames(List<UserBean> data) {
        final String[] names = {""};
        CollectionUtils.iterator(data, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                UserBean itemUser = (UserBean) data;
                names[0] += itemUser.getUserName() + ",";
                return false;
            }
        });
        if (names[0].length() > 0)
            return names[0].substring(0, names[0].length() - 1);
        return names[0];
    }

    public String getExecutorUserIds() {
        return getUserIds(receiveUsers);
    }

    private String getUserIds(List<UserBean> data) {
        final String[] ids = {""};
        CollectionUtils.iterator(data, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                UserBean itemUser = (UserBean) data;
                ids[0] += itemUser.getUserId() + ",";
                return false;
            }
        });
        if (ids[0].length() > 0)
            return ids[0].substring(0, ids[0].length() - 1);
        return ids[0];
    }

    public int getTaskId() {
        return userMissionId;
    }

    public String getFinishTimeFormat() {
        if (!StringUtils.isEmpty(finishTime))
            return finishTime.replace("年", "-").replace("月", "-").replace("日", "").replace("截止", ":00");
        else
            return finishTime;
    }

    public String getFinishTimeToShow() {
        if (!StringUtils.isEmpty(finishTime))
            if (finishTime.contains("-")) {
                String[] date = finishTime.split(" ")[0].split("-");
                String year = date[0];
                String month = date[1];
                String day = date[2];
                return year + "年" + month + "月" + day + "日"
                        + " " +
                        finishTime.split(" ")[1].substring(0, finishTime.split(" ")[1].lastIndexOf(":")) + "截止";
            }

        return finishTime;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public boolean isLive() {
        return state.equals("进行中") || state.equals("1");
    }
}
